package com.gitee.wsl.common.chart.render.shape.pointer

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path

import com.gitee.wsl.android.ext.math.calcArcEndPointXY
import com.gitee.wsl.android.ui.ext.type.lineTo
import com.gitee.wsl.common.ui.draw.type.PointerStyle
import com.gitee.wsl.math.Math
import com.gitee.wsl.jvm.math.add
import com.gitee.wsl.jvm.math.mul

/**
 * @ClassName Pointer
 * @Description 指针类,定制其属性
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 *
 */
class Pointer {
     val pointPaint: Paint by lazy{
         Paint().apply {
             color = Color.rgb(235, 138, 61)
             strokeWidth = 3f
             style = Paint.Style.FILL
             isAntiAlias = true
         }
     }
     val baseCirclePaint: Paint by lazy{
        Paint().apply {
            color = Color.rgb(235, 138, 61)
            strokeWidth = 8f
            style = Paint.Style.FILL
            isAntiAlias = true
        }
    }

     var centerX = 0.0f
     var centerY = 0.0f

     var percentage = 0.0f
     var pointerRadiusPercentage = 0.9f
     var pointerTailRadiusPercentage = 0.0f

     var baseRadius = 20f

    var pointStyle = PointerStyle.LINE
    var isShowBaseCircle = true

    var startAngle = 0.0f
    var totalAngle = 0.0f
    var pointerAngle = 0.0f

    var parentRadius = 0.0f
    var pointerRadius = 0.0f
    var pointerTailRadius = 0.0f

    var endX = 0.0f
    var endY = 0.0f

    var tailX = 0.0f
    var tailY = 0.0f

    var path: Path = Path()

    val currentPointerAngle:Float
        get() = Math.mul(totalAngle, percentage)

    
    /**
     * 设置指针长度
     * @param radiusPercentage        占总半径的比例
     * @param tailRadiusPercentage 尾部延长占总半径的比例
     */
    fun setLength(radiusPercentage: Float, tailRadiusPercentage: Float=0f) {
        pointerRadiusPercentage = radiusPercentage
        pointerTailRadiusPercentage = tailRadiusPercentage
    }

    /**
     * 设置指针的绘制的起始坐标位置
     * @param x
     * @param y
     */
    fun setStartXY(x: Float, y: Float) {
        centerX = x
        centerY = y
    }

    fun setPointEndXY(x: Float, y: Float) {
        endX = x
        endY = y
    }

    private fun calcRadius() {
        if (pointerRadiusPercentage.compareTo(0.0f) == 1) {
            pointerRadius =  Math.mul(parentRadius, pointerRadiusPercentage)
        }
        if (pointerTailRadiusPercentage.compareTo(0.0f) == 1) {
            pointerTailRadius =  Math.mul(parentRadius, pointerTailRadiusPercentage)
        }
    }

    private fun calcEndXY() {
        var pointer = Math.calcArcEndPointXY(
            centerX, centerY, pointerRadius,
            currentPointerAngle + startAngle
        )
        endX = pointer.x
        endY = pointer.y
        if (pointerTailRadiusPercentage.compareTo(0.0f) == 1) {
            val tailAgent = pointerAngle + startAngle - 180
            pointer = Math.calcArcEndPointXY(centerX, centerY, pointerTailRadius, tailAgent)
            tailX = pointer.x
            tailY = pointer.y
        } else {
            tailX = centerX
            tailY = centerY
        }
    }

    fun renderLine(canvas: Canvas) {
        canvas.drawLine(centerX, centerY, endX, endY, pointPaint)
    }

    fun renderTriangle(canvas: Canvas) {
        val currentAgent1 = Math.add(pointerAngle - FIX_ANGLE, startAngle)
        val currentAgent2 = Math.add(pointerAngle + FIX_ANGLE, startAngle)

        path.reset()
        val pointF= Math.calcArcEndPointXY(tailX, tailY, baseRadius, currentAgent1)
        val pointF1= Math.calcArcEndPointXY(tailX, tailY, baseRadius, currentAgent2)
        path.moveTo(endX, endY)
        path.lineTo(pointF)
        path.lineTo(pointF1)
        path.close()
        canvas.drawPath(path,pointPaint)
    }

    fun renderCircle(canvas: Canvas) {
        canvas.drawCircle(centerX, centerY, baseRadius,baseCirclePaint )
    }

    fun render(canvas: Canvas) {
        calcRadius()
        calcEndXY()
        when (pointStyle) {
            PointerStyle.TRIANGLE -> {
                renderTriangle(canvas)
                if (isShowBaseCircle) renderCircle(canvas)
            }
            PointerStyle.LINE -> {
                renderLine(canvas)
                if (isShowBaseCircle) renderCircle(canvas)
            }
        }
    }

    fun setCurrentAngle(currentAngle: Float) {
        pointerAngle = currentAngle
    }

    @JvmName("setStartAngle1")
    fun setStartAngle(startAngle: Float) {
        this.startAngle = startAngle
    }
    @JvmName("setTotalAngle1")
    fun setTotalAngle(totalAngle: Float) {
        this.totalAngle = totalAngle
    }

    @JvmName("setParentRadius1")
    fun setParentRadius(radius: Float) {
        parentRadius = radius
    }


    companion object{
        private const val FIX_ANGLE = 90
    }
}